package com.enqualcomm.kids.mvp.login.wx.model;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.mvp.login.wx.presenter.OnWXRegistListener;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.BindUserRelationParams;
import com.enqualcomm.kids.network.socket.request.LoginOtherParams;
import com.enqualcomm.kids.network.socket.request.LoginOtherUpdateParams;
import com.enqualcomm.kids.network.socket.response.BindUserRelationResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherResult;
import com.enqualcomm.kids.network.socket.response.LoginOtherUpdateResult;

/**
 * Created by Sunzhigang on 2016/9/8.
 */
public class WXRegistModelImpl extends NetworkModel implements WXRegistModel {
    AppDefault appDefault;

    public WXRegistModelImpl() {
        appDefault = new AppDefault();
    }

    @Override
    public void newRegist(String phone, String country, final OnWXRegistListener listener) {
        LoginOtherUpdateParams params = new LoginOtherUpdateParams(appDefault.getWXUserNickName(), appDefault.getWXUserid(), "1", country, phone);
        loadDataFromServer(new SocketRequest<LoginOtherResult>(params, new NetworkListener<LoginOtherResult>() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(LoginOtherResult response) {
                listener.registSuccess(response);
            }
        }));
    }

    @Override
    public void oldBind(String phone, String country, String userpwd, final OnWXRegistListener listener) {
        BindUserRelationParams params = new BindUserRelationParams(appDefault.getWXUserNickName(), appDefault.getWXUserid(), "1", country, phone, userpwd);
        loadDataFromServer(new SocketRequest<LoginOtherResult>(params, new NetworkListener<LoginOtherResult>() {
            @Override
            public void onError(VolleyError error) {
                listener.bindtError(R.string.app_no_connection);
            }

            @Override
            public void onSuccess(LoginOtherResult response) {
                if (response.code == 0) {
                    listener.bindSuccess(response);
                } else {
                    listener.bindtError(R.string.account_password_wrong);
                }
            }
        }));
    }
}
